/**
 * Created with IntelliJ IDEA.
 * User: Hkkkk Guo
 * Date: 2023/2/10
 * Time: 17:18
 * Description:
 */
package com.hangkai.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hangkai.reggie.common.Result;
import com.hangkai.reggie.dao.DishDao;
import com.hangkai.reggie.domain.Dish;
import com.hangkai.reggie.domain.DishFlavor;
import com.hangkai.reggie.dto.DishDto;
import com.hangkai.reggie.service.DishFlavorService;
import com.hangkai.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * /新增菜品 同时插入菜品对应的口味数据 需要操作两张表
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        // 保存菜品的基本信息到菜品表 dish
        this.save(dishDto);
        // 获取菜品的id
        // mp自动生成的id会先保存到实体中，再进行数据库的插入，所以这里可以直接获取id
        Long dishId = dishDto.getId();
        //菜品口味表 的dish_id赋值
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        // 保存菜品口味到菜品口味表 dish_flavor
        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id获取单个菜品信息 和 口味信息
     * @param id
     */
    @Override
    @Transactional
    public DishDto getByidWithFlavor(Long id) {
        //根据id查到菜品信息
        Dish dish = this.getById(id);
        if(dish == null){
            throw new RuntimeException("查不到菜品信息");
        }
        //用dishDto对象(里面含有 list属性存储flavors)
        DishDto dishDto = new DishDto();
        //将dish里的数据拷贝到dishDto里
        BeanUtils.copyProperties(dish, dishDto);
        //条件构造器
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 更新菜品 且更新菜品口味
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        Long dishid = dishDto.getId();
        //条件构造器
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        //先删除当前菜品对应的口味数据
        dishFlavorService.remove(lqw);
        //再根据提交来的口味数据做新增操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        for(DishFlavor dishFlavor:flavors){
            dishFlavor.setDishId(dishid);
        }
        dishFlavorService.saveBatch(flavors);
    }

}
